package com.polaris.live.ui.luckybag.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R
import com.polaris.live.adapter.luckybag.LuckyBagHistoryAdapter
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.FragmentLuckyBagHistoryBinding
import com.polaris.live.dialog.luckybag.LuckyBagDetailDialog
import com.polaris.live.resp.back_resp.luckybag.LuckyBagSendResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.XPopupUtils
import com.polaris.live.viewmodel.luckybag.LuckyBagHistoryViewModel

/**
 * LuckyBagHistoryFragment
 *
 * @author Created by lucas on 2024/6/11 10:02
 */
class LuckyBagHistoryFragment : BaseVbFragment<LuckyBagHistoryViewModel, FragmentLuckyBagHistoryBinding>() {

    private val mType by lazy {
        arguments?.getInt("type") ?: RECEIVED
    }

    private val mAdapter by lazy { LuckyBagHistoryAdapter() }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.rvRecycler.apply {
            setEmptyTip(R.drawable.salalogo, StringUtils.getString(string.no_hava_data))
            recycler.layoutManager = LinearLayoutManager(requireContext())
            attachedAdapter(mAdapter, mViewModel).observe(this@LuckyBagHistoryFragment, mViewModel.mListLiveData)
        }

        mViewModel.mType = mType

        mAdapter.setOnItemClickListener { _, _, position ->
            val item = mAdapter.getItemOrNull(position) ?: return@setOnItemClickListener
            if (item is LuckyBagSendResp) {
                mViewModel.getLuckyBagDetail(item.luckyBagId)
            }
        }
        initObserver()
    }

    private fun initObserver() {
        mViewModel.mDetailLuckyBagLiveData.observe(this) { resultState ->
            parseState(resultState, {
                XPopupUtils.showPopup(
                    LuckyBagDetailDialog(requireContext(), it, mViewModel),
                    { hasShadowBg(true) },
                    lifecycle = lifecycle
                )
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }

    override fun initData() {
        mBinding.rvRecycler.autoRefresh()
    }

    companion object {

        const val RECEIVED = 0          //领取
        const val SEND = 1              //发放
    }
}